<?php
require("global.php");
//get the other player
$getEnemy = $db->select("players","*","pid=".intval($phprpg->input['pid']));
if($db->recordcount($getEnemy) == 0){
	$phprpg->error_page("This player doesn't exist.");
	exit;
}
$phprpg->enemy = $db->fetchrow($getEnemy);
if($phprpg->enemy['level'] - $phprpg->player['level'] > $phprpg->settings['fight_maxdiff']){
	$phprpg->error_page("This player's level is to high for you to attack.");
	exit;
}
if($phprpg->player['level'] - $phprpg->enemy['level'] > $phprpg->settings['fight_maxdiff']){
	$phprpg->error_page("This player's level is to low for you to attack.");
	exit;
}
if($phprpg->player['pid'] == $phprpg->enemy['pid']){
	$phprpg->error_page("You can't fight yourself.");
	exit;
}
if($phprpg->player['hp'] == 0){
	$phprpg->error_page("You can't fight when you're unconscious.");
	exit;
}
if($phprpg->enemy['hp'] == 0){
	$phprpg->error_page("You can't fight someone that is unconscious.");
	exit;
}
if($phprpg->player['energy'] == 0){
	$phprpg->error_page("You can't fight when you're out of energy.");
	exit;
}
if($phprpg->enemy['energy'] == 0){
	$phprpg->error_page("You can't fight someone that is out of energy.");
	exit;
}

$attacker = 0;
$defender = 0;
$rounds = 0;
$bool = 0;
//get equipped items
$query = $db->query("select items.weight, items.bonus, items.name from `inventory`, `items` where items.id=inventory.iid and inventory.pid=? and items.type='weapon' and inventory.equipped=1", array($phprpg->enemy['pid']));
$phprpg->enemy['weapon'] = ($db->recordcount($query) == 1)?$db->fetchrow($query):0;

$query = $db->query("select items.weight, items.bonus, items.name from `inventory`, `items` where items.id=inventory.iid and inventory.pid=? and items.type='armor' and inventory.equipped=1", array($phprpg->enemy['pid']));
$phprpg->enemy['armor'] = ($db->recordcount($query) == 1)?$db->fetchrow($query):0;

$query = $db->query("select items.weight, items.bonus, items.name from `inventory`, `items` where items.id=inventory.iid and inventory.pid=? and items.type='weapon' and inventory.equipped=1", array($phprpg->player['pid']));
$phprpg->player['weapon'] = ($db->recordcount($query) == 1)?$db->fetchrow($query):0;

$query = $db->query("select items.weight, items.bonus, items.name from `inventory`, `items` where items.id=inventory.iid and inventory.pid=? and items.type='armor' and inventory.equipped=1", array($phprpg->player['pid']));
$phprpg->player['armor'] = ($db->recordcount($query) == 1)?$db->fetchrow($query):0;

//calculate new strength, defense, and agility
$phprpg->enemy['strength'] = ($phprpg->enemy['strength'] * 2) + $phprpg->enemy['weapon']['bonus'];
$phprpg->enemy['defense'] = $phprpg->enemy['defense'] + $phprpg->enemy['armor']['bonus'];
$phprpg->enemy['agility'] = $phprpg->enemy['agility'] - ( $phprpg->enemy['armor']['weight'] + $phprpg->enemy['weapon']['weight'] );
$phprpg->enemy['agility'] = ($phprpg->enemy['agility'] < 1)?1:$phprpg->enemy['agility'];

$phprpg->player['strength'] = ($phprpg->player['strength'] * 2) + $phprpg->player['weapon']['bonus'];
$phprpg->player['defense'] = $phprpg->player['defense'] + $phprpg->player['armor']['bonus'];
$phprpg->player['agility'] = $phprpg->player['agility'] - ( $phprpg->player['armor']['weight'] + $phprpg->player['weapon']['weight'] );
$phprpg->player['agility'] = ($phprpg->player['agility'] < 1)?1:$phprpg->player['agility'];

//calculate totals/differences in strength, defense, and agility
$phprpg->enemy['strdiff'] = (($phprpg->enemy['strength'] - $phprpg->player['strength']) > 0)?($phprpg->enemy['strength'] - $phprpg->player['strength']):0;
$phprpg->enemy['defdiff'] = (($phprpg->enemy['defense'] - $phprpg->player['defense']) > 0)?($phprpg->enemy['defense'] - $phprpg->player['defense']):0;
$phprpg->enemy['agidiff'] = (($phprpg->enemy['agility'] - $phprpg->player['agility']) > 0)?($phprpg->enemy['agility'] - $phprpg->player['agility']):0;

$phprpg->player['strdiff'] = (($phprpg->player['strength'] - $phprpg->enemy['strength']) > 0)?($phprpg->player['strength'] - $phprpg->enemy['strength']):0;
$phprpg->player['defdiff'] = (($phprpg->player['defense'] - $phprpg->enemy['defense']) > 0)?($phprpg->player['defense'] - $phprpg->enemy['defense']):0;
$phprpg->player['agidiff'] = (($phprpg->player['agility'] - $phprpg->enemy['agility']) > 0)?($phprpg->player['agility'] - $phprpg->enemy['agility']):0;

$totalstr = $phprpg->enemy['strength'] + $phprpg->player['strength'];
$totaldef = $phprpg->enemy['defense'] + $phprpg->player['defense'];
$totalagi = $phprpg->enemy['agility'] + $phprpg->player['agility'];

//calculate max hit
$phprpg->enemy['maxdmg'] = $phprpg->enemy['strength'];
$phprpg->enemy['maxdmg'] = $phprpg->enemy['maxdmg'] - intval($phprpg->enemy['maxdmg'] * ($phprpg->player['defdiff'] / $totaldef));
$phprpg->enemy['maxdmg'] = ($phprpg->enemy['maxdmg'] <= 2)?2:$phprpg->enemy['maxdmg'];
$phprpg->enemy['mindmg'] = (($phprpg->enemy['maxdmg'] - 4) < 1)?1:($phprpg->enemy['maxdmg'] - 4);

$phprpg->player['maxdmg'] = $phprpg->player['strength'];
$phprpg->player['maxdmg'] = $phprpg->player['maxdmg'] - intval($phprpg->player['maxdmg'] * ($phprpg->enemy['defdiff'] / $totaldef));
$phprpg->player['maxdmg'] = ($phprpg->player['maxdmg'] <= 2)?2:$phprpg->player['maxdmg'];
$phprpg->player['mindmg'] = (($phprpg->player['maxdmg'] - 4) < 1)?1:($phprpg->player['maxdmg'] - 4);

while($phprpg->player['hp'] > 0 && $phprpg->enemy['hp'] > 0 && $rounds < $phprpg->settings['fight_maxrounds']){
	if($bool==0){
		$attacker = $phprpg->player;
		$defender = $phprpg->enemy;
		$bool = 1;
	}else{
		$attacker = $phprpg->enemy;
		$defender = $phprpg->player;
		$bool = 0;
	}
	++$rounds;
	$damage = rand($attacker['mindmg'], $attacker['maxdmg']);
	$defender['hp'] = ($defender['hp'] - $damage < 0)?0:$defender['hp']-$damage;
	$fightdetails .= "{$attacker['username']} hit {$defender['username']} and did {$damage} damage ({$defender['hp']}hp remaining)<br />\n";
	if($bool==1){
		$phprpg->player = $attacker;
		$phprpg->enemy = $defender;
	}else{
		$phprpg->player = $defender;
		$phprpg->enemy = $attacker;
	}
}

$cashwin = floor(0.25 * $defender['money']);
$expwin1 = $defender['level'] * 6;
$expwin2 = (($attacker['level'] - $defender['level']) > 0)?$expwin1 - (($attacker['level'] - $defender['level']) * 3):$expwin1 + (($attacker['level'] - $defender['level']) * 3);
$expwin2 = ($expwin2 <= 0)?1:$expwin2;
$expwin3 = round(0.6 * $expwin2);
$expwin = ceil(rand($expwin3, $expwin2));

if($phprpg->enemy['hp'] == 0){
	$fightdetails .= "You won the fight and gained {$cashwin} gold and {$expwin} experience!<br />\n";
	$update_enemy['hp'] = 0;
	$update_enemy['energy'] = $phprpg->enemy['energy'] - 1;
	$db->update("players",$update_enemy,"pid={$phprpg->enemy['pid']}");

	$update_player['exp'] = $phprpg->player['exp'] + $expwin;
	if($update_player['exp'] >= $phprpg->player['maxexp']){
		$update_player['level'] = $phprpg->player['level'] + 1;
		$update_player['exp'] = $update_player['exp'] - $phprpg->player['max_exp'];
		$update_player['maxexp'] = $update_player['level'] * 10;
		$update_player['points'] = $phprpg->player['points'] + 3;
		$fightdetails .= "You have leveled up to level ".$update_player['level']."!";
	}
	$update_player['hp'] = $phprpg->player['hp'];
	$update_player['energy'] = $phprpg->player['energy'] - 1;
	$update_player['money'] = $phprpg->player['money'] + $cashwin;
	$db->update("players",$update_player,"pid={$phprpg->player['pid']}");
}elseif($phprpg->player['hp'] == 0){
	$fightdetails .= "You lost the fight and lost {$cashwin} gold.";
	$update_player['hp'] = 0;
	$update_player['energy'] = $phprpg->player['energy'] - 1;
	$update_player['money'] = $phprpg->player['money'] - $cashwin;
	$db->update("players",$update_player,"pid={$phprpg->player['pid']}");

	$update_enemy['hp'] = $phprpg->enemy['hp'];
	$update_enemy['energy'] = $phprpg->enemy['energy'] - 1;
	$update_enemy['exp'] = $phprpg->enemy['exp'] + $expwin;
	if($update_enemy['exp'] >= $phprpg->enemy['maxexp']){
		$update_enemy['level'] = $phprpg->enemy['level'] + 1;
		$update_enemy['exp'] = $update_enemy['exp'] - $phprpg->enemy['max_exp'];
		$update_enemy['maxexp'] = $update_enemy['level'] * 10;
		$update_enemy['points'] = $phprpg->enemy['points'] + 3;
	}
	$update_enemy['money'] = $phprpg->enemy['money'] + $cashwin;
	$db->update("players",$update_enemy,"pid={$phprpg->enemy['pid']}");
}else{
	$fightdetails .= "The fight ended in a draw.";
}
eval("\$fight = \"".$templates->get("fight")."\";");
$templates->output($fight);
?>